package com.nice.nice201110.controller;

import com.nice.nice201110.entity.TBasePicture;
import com.nice.nice201110.service.TbasePictureService;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@RequestMapping("/admin/picture")
public class TbasePictureComtroller {

    @Autowired
    TbasePictureService tbasePictureService;

    private Map<String,Object> codeMes = new HashMap<>();


    /** 查询所有数据 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/list")
    public Map<String,Object> list(HttpServletRequest request, BaseVo baseVo){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code", NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        if(baseVo!=null){
            baseVo.setUservo(userVo);
        }else{
            baseVo = new BaseVo();
            baseVo.setUservo(userVo);
        }
        List<TBasePicture> tBasePictures = tbasePictureService.getList(baseVo);

        codeMes.put("code", NiceEnum.code_200);
        codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        codeMes.put("data", tBasePictures);
        return codeMes;

    }
    /** 查询单一的数据 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/one")
    public Map<String,Object> one(HttpServletRequest request, Integer id){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code", NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        TBasePicture tBasePicture = tbasePictureService.getOne(userVo,id);
        if(tBasePicture!=null) {
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBasePicture);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_102);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_102));
        return codeMes;
    }
    /**更新信息*/
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT,value="/update")
    public Map<String,Object> updateTbasePicture(HttpServletRequest request,@RequestBody TBasePicture tBasePicture){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbasePictureService.updateTbasePicture(userVo,tBasePicture);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     *  插入数据
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/insert")
    public Map<String,Object> insetTbasePicture(HttpServletRequest request, @RequestBody(required = false) TBasePicture tBasePicture){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes = new HashMap<>();
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbasePictureService.insertTbasePicture(userVo,tBasePicture);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     *  删除数据
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value="/delete")
    public Map<String,Object> deleteTbasePicture(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbasePictureService.deleteTbasePicture(userVo,id);
        if(blean){
            codeMes.put("code",NiceEnum.code_200);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /**
     * 删除数据s
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value="/deletes")
    public Map<String,Object> deletesTbasePicture(HttpServletRequest request,@RequestParam(name = "ids",required=true) String ids){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbasePictureService.deletesTbasePicture(userVo,ids);
        if (blean) {
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            return codeMes;

        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
}
